-- Copyright (c) 2025 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.

local test_utils = require 'test_utils'
local lu = require 'luaunit'

local cert_enum = require 'spdm.json_types.Certificate'

local certificate_suit = {}
certificate_suit.__index = certificate_suit

local CHASSIS_ID = 1
local MDB_NAME = "SPDM_0"

local SLOT_ID = 1

local mock_certificate = {
    ["CertificateType"] = 0,
    ["CertificateUsageType"] = cert_enum.CertificateUsageType.TrustedComponentCertificate:value(),
    ["Fingerprint"] = "ca035e3805da28424d9fd3c938c7240abc6c52d6a78def1cac5f09c8134a0920",
    ["FingerprintHashAlgorithm"] = "TCG_ALG_SHA256",
    ["Issuer"] = "CN=TestCA, OU=IT, O=HW, L=CD, S=SC, C=CN",
    ["Subject"] = "CN=TestCA, OU=IT, O=HW, L=CD, S=SC, C=CN",
    ["SerialNumber"] = "6c 7f 0d ff 1f 94 fc c2 12 43 94 00 b5 81 c5 9a 9e 77 74 e1",
    ["SignatureAlgorithm"] = "sha256WithRSAEncryption",
    ["ValidNotAfter"] = "Nov 08 2075 UTC",
    ["ValidNotBefore"] = "Nov 20 2025 UTC",
    ["FilePath"] = "",
    ["CommonName"] = "TestCA",
    ["CRLStartTime"] = "",
    ["CRLExpireTime"] = "",
    ["CertCount"] = 1,
    ["KeyLength"] = 4096, -- 预期私钥长度4096
}

function certificate_suit.test_get_certificate(bus, test_data_dir)
    for k, v in pairs(mock_certificate) do
        lu.assertEquals(
            test_utils.get_certificate_property(bus, CHASSIS_ID, MDB_NAME, SLOT_ID, k),
            v
        )
    end

    local key_usage = test_utils.get_certificate_property(bus, CHASSIS_ID, MDB_NAME, SLOT_ID, "KeyUsage")
    lu.assertEquals(#key_usage, 0)
end

return certificate_suit